package com.hdax.controller;


import com.hdax.entity.Permission;
import com.hdax.service.PermissionService;
import com.hdax.until.UserIdUntil;
import com.hdax.vo.PermissionVo;
import com.hdax.vo.TreeTableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService service;

    @RequestMapping("/toAddGrantPage")
    public ModelAndView toAddGrantPage(Integer roleId) {
        ModelAndView mv = new ModelAndView("role/grant");
        mv.addObject("roleId", roleId);
        return mv;
    }

    @PostMapping("/queryGrant")
    public List<PermissionVo> queryGrant(Integer roleId) {
        List<PermissionVo> permissionVos = service.selectAllPermission(roleId);
        return permissionVos;
    }

    @RequestMapping("/addGrant")
    public String addGrant(@RequestParam("roleId") Integer roleId, @RequestParam("permissions") List<Integer> permissions) {
        service.deleteByRid(roleId);
        if (service.addPermission(roleId, permissions) > 0) {
            return "授权成功";
        } else {
            return "授权失败";
        }
    }

    @GetMapping("/index")
    public ModelAndView openListPage() {
        return new ModelAndView("permission/list");
    }

    @RequestMapping("/list")
    public TreeTableVo selectAllPermission() {
        TreeTableVo treeTableVo = new TreeTableVo();
        List<Permission> permissions = service.selectPermission();
        treeTableVo.setData(permissions);
        return treeTableVo;
    }

    @RequestMapping("/addPermissionPage")
    public ModelAndView addPermissionPage(Integer grade, Integer parent) {
        ModelAndView mv = new ModelAndView("permission/add");
        mv.addObject("parent", parent);
        mv.addObject("grade", grade);
        return mv;
    }

    @PostMapping("/save")
    public int save(Permission permission) {
        permission.setCreateTime(LocalDateTime.now());
        permission.setCreateUserId(UserIdUntil.getUserId());
        return service.addPermission(permission);
    }

    @RequestMapping("/updatePermissionPage")
    public ModelAndView updatePermissionPage(Integer id) {
        ModelAndView mv = new ModelAndView("permission/update");
        Permission permission = service.selectById(id);
        mv.addObject("permission", permission);
        return mv;
    }

    @PostMapping("/update")
    public int updatePermission(Permission permission) {
        permission.setUpdateTime(LocalDateTime.now());
        permission.setUpdateUserId(UserIdUntil.getUserId());
        return service.updatePermission(permission);
    }

    @PostMapping("/delete")
    public String delete(Integer id) {
        if (service.selectCountById(id) > 0) {
            return "该权限下有子权限，不能删除";
        } else {
            if (service.deletePermission(id) > 0) {
                return "删除成功";
            } else {
                return "删除失败";
            }
        }
    }
}

